import 'package:app_30/home/fragment/vm/share_vm.dart';
import 'package:app_30/home/share/share_qr_page.dart';
import 'package:app_30/res/colors.dart';
import 'package:app_30/util/toast_utils.dart';
import 'package:app_30/widgets/my_app_bar.dart';
import 'package:app_30/widgets/my_scroll_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:stacked/stacked.dart';
import 'package:url_launcher/url_launcher.dart';

class SharePage extends StatefulWidget {
  const SharePage({Key? key}) : super(key: key);

  @override
  State<SharePage> createState() => _SharePageState();
}

class _SharePageState extends State<SharePage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    return ViewModelBuilder<ShareViewModel>.reactive(
        viewModelBuilder: () => ShareViewModel(),
        onModelReady: (viewModel) => viewModel.initialise(),
        builder: (context, viewModel, child) => Scaffold(
            appBar: const MyAppBar(
              isBack: false,
              centerTitle: '分享',
            ),
            body: MyScrollView(
              children: [
                Container(
                  color: Colors.white,
                  child: Padding(
                      padding: EdgeInsets.all(15),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisSize: MainAxisSize.max,
                            children: [
                              Image.asset(
                                "assets/image/tuiguang.png",
                                width: 13,
                                height: 13,
                              ),
                              const Padding(
                                padding: EdgeInsets.only(left: 5),
                                child: Text(
                                  "推广方式A：推荐别人访问你的推广连接",
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                      fontSize: 16, color: Colors.black54),
                                ),
                              )
                            ],
                          ),
                          buildPadding(),
                          const Text(
                            "此推广方式需要您通过分享推广内容给朋友，只要有人点击了您的推广链接，您就会获取到1个推广积分，对方无论是否下载APP都是计数的.当您的推广积分满足升级所需积分，您即可在VIP申请界面申请升级VIP等级",
                            textAlign: TextAlign.left,
                            style:
                                TextStyle(fontSize: 14, color: Colors.black54),
                          ),
                          buildPadding(),
                          Text(
                            "·我的推广积分：${viewModel.tgjifen}积分（够${viewModel.tgfennum}积分，可获得1个月VIP）",
                            textAlign: TextAlign.left,
                            style:
                                TextStyle(fontSize: 14, color: Colors.orange),
                          ),
                          SizedBox(
                            height: 6,
                          ),
                          Container(
                              padding: EdgeInsets.all(5),
                              color: Color(0x551CC6B9),
                              child: Text(
                                "${viewModel.tgnr}",
                                textAlign: TextAlign.left,
                                style:
                                    TextStyle(fontSize: 14, color: Colors.grey),
                              )),
                          SizedBox(
                            height: 10,
                          ),
                          Row(mainAxisSize: MainAxisSize.max, children: [
                            Expanded(
                                child: SizedBox(
                              height: 30,
                              child: TextButton(
                                child: Text("查询推广积分"),
                                //添加一个点击事件
                                onPressed: viewModel.searchTgjf,
                                //设置按钮是否自动获取焦点
                                autofocus: true,
                                //定义一下文本样式
                                style: buildButtonStyle(),
                              ),
                            )),
                            SizedBox(
                              width: 10,
                            ),
                            Expanded(
                                child: // use Spacer
                                    SizedBox(
                              height: 30,
                              child: TextButton(
                                child: Text("复制推广信息"),
                                //添加一个点击事件
                                onPressed: () {
                                  Clipboard.setData(
                                      ClipboardData(text: '${viewModel.tgnr}'));
                                  Toast.show('复制成功，可以发给朋友们了。');
                                },
                                //设置按钮是否自动获取焦点
                                autofocus: true,
                                //定义一下文本样式
                                style: buildButtonStyle(),
                              ),
                            )),
                            SizedBox(
                              width: 10,
                            ),
                            Expanded(
                                child: // use Spacer
                                    SizedBox(
                              height: 30,
                              child: TextButton(
                                child: Text("推广教程"),
                                //添加一个点击事件
                                onPressed: () {
                                  launch('${viewModel.gongGao!.vipspurl}');
                                },
                                //设置按钮是否自动获取焦点
                                autofocus: true,
                                //定义一下文本样式
                                style: buildButtonStyle(),
                              ),
                            ))
                          ]),
                          Padding(
                            padding: EdgeInsets.only(top: 10, bottom: 10),
                            child: SizedBox(
                              width: double.infinity,
                              height: 10,
                              child: Container(
                                color: Colors.black12,
                              ),
                            ),
                          ),
                          Row(
                            mainAxisSize: MainAxisSize.max,
                            children: [
                              Image.asset(
                                "assets/image/fenxiang2.png",
                                width: 13,
                                height: 13,
                              ),
                              const Padding(
                                padding: EdgeInsets.only(left: 5),
                                child: Text(
                                  "推广方式B：邀请别人成功注册",
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                      fontSize: 16, color: Colors.black54),
                                ),
                              )
                            ],
                          ),
                          buildPadding(),
                          const Text(
                            "此推广方式要求您介绍的用户下载并且在注册的时候，在推荐人之中填写您的APP用户名，也就是您的APP登入账号，成功注册即可.\n当您邀请的“未结算推荐人”满足升级VIP条件，即可在升级VIP界面申请升级VIP.\n成功推广用户注册还能赚取到现金奖励，奖励可在“推荐奖励”里面提现到你的支付宝之中.",
                            textAlign: TextAlign.left,
                            style:
                                TextStyle(fontSize: 14, color: Colors.black54),
                          ),
                          buildPadding(),
                          Text(
                            //
                            "·我的注册人数：${viewModel.tjnum} 人（够${viewModel.tjrnum} 人，可获得终身VIP）",
                            textAlign: TextAlign.left,
                            style:
                                TextStyle(fontSize: 14, color: Colors.orange),
                          ),
                          const SizedBox(
                            height: 6,
                          ),
                          Row(mainAxisSize: MainAxisSize.max, children: [
                            Expanded(
                                child: SizedBox(
                              height: 30,
                              child: TextButton(
                                child: Text("查询注册人数"),
                                //添加一个点击事件
                                onPressed: viewModel.searchRegistnum,
                                //设置按钮是否自动获取焦点
                                autofocus: true,
                                //定义一下文本样式
                                style: buildButtonStyle(),
                              ),
                            )),
                            const SizedBox(
                              width: 10,
                            ),
                            Expanded(
                                child: // use Spacer
                                    SizedBox(
                              height: 30,
                              child: TextButton(
                                child: Text("一键分享"),
                                //添加一个点击事件
                                onPressed: () {
                                  Navigator.of(context).push(MaterialPageRoute(
                                    builder: (context) {
                                      return ShareQrPage();
                                    },
                                  ));
                                },
                                //设置按钮是否自动获取焦点
                                autofocus: true,
                                //定义一下文本样式
                                style: buildButtonStyle(),
                              ),
                            ))
                          ])
                        ],
                      )),
                )
              ],
            )));
  }

  Padding buildPadding() {
    return Padding(
      padding: EdgeInsets.only(top: 10, bottom: 10),
      child: SizedBox(
        width: double.infinity,
        height: 1,
        child: Container(
          color: Colors.grey,
        ),
      ),
    );
  }

  ButtonStyle buildButtonStyle() {
    return ButtonStyle(
      //定义文本的样式 这里设置的颜色是不起作用的
      textStyle: MaterialStateProperty.all(
          TextStyle(fontSize: 15, color: Colours.app_main)),
      //设置按钮上字体与图标的颜色
      //foregroundColor: MaterialStateProperty.all(Colors.deepPurple),
      //更优美的方式来设置
      foregroundColor: MaterialStateProperty.resolveWith(
        (states) {
          if (states.contains(MaterialState.focused) &&
              !states.contains(MaterialState.pressed)) {
            //获取焦点时的颜色
            return Colors.white;
          } else if (states.contains(MaterialState.pressed)) {
            //按下时的颜色
            return Colors.white70;
          }
          //默认状态使用灰色
          return Colors.white;
        },
      ),
      //背景颜色
      backgroundColor: MaterialStateProperty.resolveWith((states) {
        //设置按下时的背景颜色
        if (states.contains(MaterialState.pressed)) {
          return Colours.dark_app_main;
        }
        //默认不使用背景颜色
        return Colours.app_main;
      }),
      //设置水波纹颜色
      overlayColor: MaterialStateProperty.all(Colours.app_main),
      //设置阴影  不适用于这里的TextButton
      elevation: MaterialStateProperty.all(0),
      //设置按钮内边距
      padding: MaterialStateProperty.all(EdgeInsets.zero),
      //设置按钮的大小
      // minimumSize: MaterialStateProperty.all(Size(double.infinity, 20)),
      //设置边框
      // side: MaterialStateProperty.all(BorderSide(color: Colors.grey, width: 1)),
      //外边框装饰 会覆盖 side 配置的样式
      // shape: MaterialStateProperty.all(StadiumBorder()),
    );
  }
}
